package com.smartTerminal.perMatters.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartTerminal.perMatters.entity.Bussiness;
import com.smartTerminal.perMatters.entity.Items;
import com.smartTerminal.perMatters.entity.Page;
import com.smartTerminal.perMatters.service.GetOrItemsService;
import com.smartTerminal.utils.Base64Util;
import com.smartTerminal.utils.UploadUtil;
import com.smartTerminal.webservice.TerminalServices;
/**
 * 
 * ClassName: GetOrItemsServiceImpl
 * @Description: TODO
 * @author zhang yachao
 * @Description: TODO
 * @date: 2018年1月9日 下午2:53:13
 */
@Service
public class GetOrItemsServiceImpl implements GetOrItemsService {
	
	//获取个人办事事项
	public String getServiceOrItems(String in0,String in1){
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getServiceOrItems(in0, in1);
	}
	
	/**
	 * 获取事项类型获取该类型下的所有事项列表
	 */
	public String getItemByTagId(String type, String tagId, String itemName,
			String currentpage, String pagesize, String token) {
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getItemByTagId(type, tagId, itemName, currentpage, pagesize, token);
	}
	
	public List<Items> getItemPageforName(String itemName,String currentpage,String pagesize,Page page){
		String context = TerminalServices.getInstance().getTerminalServicesHttpPort().getItemByName(itemName, currentpage, pagesize);
		JSONObject json = JSONObject.parseObject(context).getJSONObject("DATA");
		int currentRow = (int) Math.ceil(Double.valueOf(json.getIntValue("ROWS"))/Double.valueOf(Integer.valueOf(pagesize)));
		page.setCurrentRow(currentRow);
		List<Items> list  = JSONArray.parseArray(json.getString("DATA"), Items.class);
		return list;
	}
	
	/**
	 * 根据用户名查询用户信息
	 */
	@Override
	public String getUserByIdInfo(String username) {
		
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getUserByIdInfo(username);
	}

	/**
	 * 根据事项id获取事办事指南信息接口
	 */
	@Override
	public String getItem(String token, String itemId) {
		
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getItem(token, itemId);
	}

	/**
	 * 网上办事-查询当前办理人、办理事项的基本信息
	 */
	@Override
	public String getUserOrItem(String token, String itemId) {
		
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getUserOrItem(token, itemId);
	}

	/**
	 * 网上办事-保存事项申报基本信息
	 */
	@Override
	public String saveItemInfo(String token, String xml) {
		
		return TerminalServices.getInstance().getTerminalServicesHttpPort().saveItemInfo(token, xml);
	}

	public String check(String name,String phone,String agentId){
		String error = null;
		//String regexPhone = "/^1[3|4|5|7|8][0-9]{9}$/";
		//String agentIdAgentId = "";
		if(name.length() == 0){
			error = "收件人不能为空";
		}
		//System.out.println(phone.matches(regexPhone));
		/*if(!phone.matches(regexPhone)){
			error = "手机号码格式有误";
		}*/
		if(phone.length() == 0 || phone.length()>11){
			error = "手机号码格式有误";
		}
		if(agentId.length() == 0){
			error = "身份证号码格式有误";
		}
		return error;
	}
	
	/**
	 * 网上办事-动态加载事项表单
	 */
	@Override
	public String queryItemDynamicForm(String itemId, String prokeyid) {
		
		return TerminalServices.getInstance().getTerminalServicesHttpPort().queryItemDynamicForm(itemId, prokeyid);
	}

	/**
	 * 网上办事-保存动态表单的信息
	 */
	@Override
	public String saveItemDynamicForm(String sb) {

		return TerminalServices.getInstance().getTerminalServicesHttpPort().saveItemDynamicForm(sb);
	}

	/**
	 * 网上办事-通过事项id获取该事项所需上传的材料
	 */
	@Override
	public String getItemFileList(String itemId) {
		
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getItemFileList(itemId);
	}

	/**
	 * 事项申报提交审核
	 * @param itemId
	 * @param prokeyid
	 * @param username
	 * @return
	 */
	public String submitItemAudit(String itemId, String prokeyid,
			String username) throws Exception{
		if(itemId==null){
			new Exception("事项id不能为空");
		}
		if(prokeyid==null){
			new Exception("业务id不能为空");
		}
		if(username==null){
			new Exception("用户名不能为空");
		}
		return TerminalServices.getInstance().getTerminalServicesHttpPort().submitItemAudit(itemId, prokeyid, username);
	}

	/**
	 * 事项材料上传（注意，此接口为HTTP接口）
	 */
	@Override
	public String uploadItemFile(String token, String itemid, String proKeyId,
			String clid, String clName, List<String> list) {
		String fileName = "base64.jpg";
		File file = null;
		List<Map<String,File>> listMap = new ArrayList<Map<String,File>>();
		Map<String,File> fileMap = new HashMap<>();
		for(String str: list){
			String serverFileName = UUID.randomUUID().toString()+".png";
			try {
				file = Base64Util.base64ToFile(str, serverFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
			fileMap.put(serverFileName, file);
			listMap.add(fileMap);
		}
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("TOKEN", token);
		map.put("ITEMID", itemid);
		map.put("PROKEYID", proKeyId);
		map.put("FILEID", clid);
		map.put("ITEMFILENAME", clName);
		String url ="http://218.87.176.156:8080/platform/UploadItemFileServlet";
		String data = null;		
		try {
			data =  UploadUtil.post(url,listMap, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

	
	
	/**
	 * 添加和修改登录用户收货地址信息
	 */
	@Override
	public String modifyAddress(String token, String userid, String string,
			String string2, String phone, String isDefault, String name,
			String telNo, String yzbm, String cmbProvince, String cmbCity) {
		return TerminalServices.getInstance().getTerminalServicesHttpPort().modifyAddress(token, userid, string, string2, phone, isDefault, name, "", yzbm, cmbProvince, cmbCity);
	}
	
	/**
	 * 办理材料寄送
	 */
	@Override
	public String materialOrderEms(String token, String xml) {
		return TerminalServices.getInstance().getTerminalServicesHttpPort().materialOrderEms(token, xml);
	}

	/**
	 * 获取第一批办事事项
	 */
	@Override
	public String getOneItemList(String itemname,String currentpage,String pagesize ){
		return TerminalServices.getInstance().getTerminalServicesHttpPort().getOneItemList("", "", "");
	}

	
	

	
	

	

	
}
